<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .wrap {
            width: 500px;
            height: 350px;
            border: 2px solid #333;
            margin: 50px 550px;
        }

        .img {
            width: 100%;
            height: 280px;
            background-color: #6ff;
            position: relative;
        }

        .img img {
            width: 100%;
            height: 100%;
        }

        .img .box-a {
            width: 50px;
            height: 50px;
            border: 1px solid #333;
            background-color: #f66;
            position: absolute;
            z-index: 2;
            background: url(../images/1.jpg) left 0px top 0px no-repeat;
            /* background-position: 30px 40px; */
        }

        .img .box-b {
            width: 50px;
            height: 50px;
            /* border: 2px solid #333; */
            background-color: #000;
            position: absolute;
            left: 300px;
            top: 150px;
        }

        .bar {
            width: 100%;
            height: 50px;
            margin-top: 8px;
            border: 1px solid #333;
            border-left: 0;
            border-right: 0;
            position: relative;
        }

        .bar .box-c {
            width: 50px;
            height: 50px;
            background-color: #f66;
            position: absolute;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <div class="img">
            <img src="../images/1.jpg" alt="">
            <div class="box-a"></div>
            <div class="box-b"></div>
        </div>
        <div class="bar">
            <div class="box-c"></div>
        </div>
    </div>
</body>
<script>
    var boxC = document.getElementsByClassName("box-c")[0];
    var bar = document.getElementsByClassName("bar")[0];
    var boxA = document.getElementsByClassName("box-a")[0];
    var boxB = document.getElementsByClassName("box-b")[0];

    var maxLeft = bar.clientWidth - boxC.clientWidth;
    var maxTop = bar.clientHeight - boxC.clientHeight;
    var x, y;

    //随机一个位置
    var randomX = Math.round(60 + Math.random() * 400);
    var randomY = Math.round(Math.random() * 200);
    // console.log(randomX);
    boxB.style.left = randomX + "px";
    boxB.style.top = randomY + "px";
    boxA.style.top = randomY + "px";


    // var a = -randomX*3.84;
    // var b = -randomY*4.28;
    // boxA.style.cssText = `backgroung-position: ${a}px ${b}px;`


    boxC.onmousedown = function (e) {
        var e = e || window.event;
        var posX = e.offsetX;
        var posY = e.offsetY;

        bar.onmousemove = function (e) {
            var e = e || window.event;
            x = e.pageX - bar.offsetLeft - posX;
            y = e.pageY - bar.offsetTop - posY;
            // console.log(e.pageX, e.pageY);
            // console.log(x, y);

            if (x < 0) x = 0;
            if (y < 0) y = 0;
            if (x > maxLeft) x = maxLeft;
            if (y > maxTop) y = maxTop;

            boxC.style.left = x + "px";
            boxC.style.top = y + "px";

            boxA.style.left = x + "px";

        }

        document.onmouseup = function () {
            if (x >= randomX - 2 && x <= randomX + 2) {
                // console.log("验证成功！");
                // boxC.textContent = "验证成功！";
                alert("验证成功！");
            } else {
                boxC.style.left = "0";
                boxA.style.left = "0";
                // alert("验证失败！");
                // boxC.textContent = "验证失败！";
            }
            bar.onmousemove = null;
        }
    }

    4.28
    3.84
</script>

</html>